/**
Company: Sarkar software technologys
WebSite: http://www.sarkar.cn
Author: yeganaaa
Date : 8/1/18
Time: 4:48 AM
 */

package com.ug_project.dict.engine.indextable

import com.ug_project.dict.engine.entity.DictItem

class DataBlock(indexTable: IndexTable, blockId: Int) : Block(indexTable, blockId){

    var previousBlockId = 0
    var dataIndex = 0
    var nextBlockId = 0
    private var _nextWordUnicode: Short = 0
    private var _previousWordUnicode: Short = 0
    var previousWordUnicode: Int
        get() = (_previousWordUnicode + Short.MAX_VALUE)
        set(value) {
            _previousWordUnicode = (value - Short.MAX_VALUE).toShort()
        }
    var nextWordUnicode: Int
        get() = (_nextWordUnicode + Short.MAX_VALUE)
        set(value) {
            _nextWordUnicode = (value - Short.MAX_VALUE).toShort()
        }


    fun load() {
        buffer.apply {
            rewind()
            previousBlockId = int
            dataIndex = int
            nextBlockId = int
            _previousWordUnicode = short
            _nextWordUnicode = short
        }

        if (_previousWordUnicode == 0.toShort()) previousWordUnicode = 0
        if (_nextWordUnicode == 0.toShort()) nextWordUnicode = 0
    }

    override fun writeToChannel() = autoWriteToChannel {
        putInt(previousBlockId)
        putInt(dataIndex)
        putInt(nextBlockId)
        putShort(_previousWordUnicode)
        putShort(_nextWordUnicode)
    }

//    override fun iterator(): Iterator<DataBlock> = DataBlockIterator(this)

    init {
        load()
    }
}